package com.ruoyi.lxzx.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.lxzx.appDTO.lxlbDTO.SqrqshDTO;
import com.ruoyi.lxzx.appVO.lxqlVO.GroupMemberListVO;
import com.ruoyi.lxzx.domain.Group;
import com.ruoyi.lxzx.domain.UserCard;
import com.ruoyi.lxzx.service.IGroupService;
import com.ruoyi.lxzx.service.IUserAttentService;
import com.ruoyi.lxzx.service.IUserCardService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.lxzx.mapper.GroupMemberMapper;
import com.ruoyi.lxzx.domain.GroupMember;
import com.ruoyi.lxzx.service.IGroupMemberService;

/**
 * 老乡圈成员Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-26
 */
@Service
@AllArgsConstructor
public class GroupMemberServiceImpl implements IGroupMemberService {
    private final GroupMemberMapper groupMemberMapper;
    private final ISysUserService userService;
    private final IUserCardService userCardService;
    private final IGroupService groupService;
    private final IUserAttentService userAttentService;

    /**
     * 查询老乡圈成员
     *
     * @param id 老乡圈成员主键
     * @return 老乡圈成员
     */
    @Override
    public GroupMember selectGroupMemberById(Long id) {
        return groupMemberMapper.selectGroupMemberById(id);
    }

    /**
     * 查询老乡圈成员列表
     *
     * @param groupMember 老乡圈成员
     * @return 老乡圈成员
     */
    @Override
    public List<GroupMember> selectGroupMemberList(GroupMember groupMember) {
        return groupMemberMapper.selectGroupMemberList(groupMember);
    }

    /**
     * 新增老乡圈成员
     *
     * @param groupMember 老乡圈成员
     * @return 结果
     */
    @Override
    public int insertGroupMember(GroupMember groupMember) {
        groupMember.setCreateTime(DateUtils.getNowDate());
        return groupMemberMapper.insertGroupMember(groupMember);
    }

    /**
     * 修改老乡圈成员
     *
     * @param groupMember 老乡圈成员
     * @return 结果
     */
    @Override
    public int updateGroupMember(GroupMember groupMember) {
        groupMember.setUpdateTime(DateUtils.getNowDate());
        return groupMemberMapper.updateGroupMember(groupMember);
    }

    /**
     * 批量删除老乡圈成员
     *
     * @param ids 需要删除的老乡圈成员主键
     * @return 结果
     */
    @Override
    public int deleteGroupMemberByIds(Long[] ids) {
        return groupMemberMapper.deleteGroupMemberByIds(ids);
    }

    /**
     * 删除老乡圈成员信息
     *
     * @param id 老乡圈成员主键
     * @return 结果
     */
    @Override
    public int deleteGroupMemberById(Long id) {
        return groupMemberMapper.deleteGroupMemberById(id);
    }

    @Override
    public List<GroupMemberListVO> qcy(Long groupId) {
        Group g = groupService.selectGroupById(groupId);
        if (g == null){
            throw new ServiceException("没有相关数据");
        }
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
        }
        List<GroupMember> groupMemberList = new ArrayList<>();
        if (g.getUserId().equals(finalUserId) || finalUserId == 0L) {
            groupMemberList = groupMemberMapper.selectGroupMemberList1(groupId);
        } else {
            groupMemberList = groupMemberMapper.selectGroupMemberList2(groupId);
        }

        GroupMember groupMember = new GroupMember();
        groupMember.setGroupId(groupId);
        List<GroupMemberListVO> result = new ArrayList<GroupMemberListVO>();
        Long finalUserId1 = finalUserId;
        groupMemberList.forEach(r -> {
            GroupMemberListVO groupMemberListVO = new GroupMemberListVO();
            BeanUtils.copyProperties(r, groupMemberListVO);
            groupMemberListVO.setName(userService.selectUserById(groupMemberListVO.getUserId()).getNickName());
            UserCard uc = userCardService.findByUserId(groupMemberListVO.getUserId());
            groupMemberListVO.setImg(uc.getImg());
            groupMemberListVO.setTel(uc.getTel());
            groupMemberListVO.setProfession(uc.getProfession());
            groupMemberListVO.setIndustry(uc.getIndustry());
            groupMemberListVO.setCompName(uc.getCompName());
            groupMemberListVO.setAddressProv(uc.getAddressProv());
            groupMemberListVO.setAddressCity(uc.getAddressCity());
            groupMemberListVO.setAddressPresent(uc.getAddressPresent());
            groupMemberListVO.setIsReal(uc.getIsReal());
            groupMemberListVO.setIsCard(uc.getIsCard());

            if ((r.getUserId() + "").equals(g.getCreateBy())) {
                groupMemberListVO.setIsGroupOwner("1");
                groupMemberListVO.setIsOwn("1");
                groupMemberListVO.setGzStatus("0");
            } else {
                groupMemberListVO.setIsGroupOwner("0");
                groupMemberListVO.setIsOwn("1");
                if (0L != finalUserId1) {
                    if (!r.getUserId().equals(finalUserId1)) {
                        groupMemberListVO.setGzStatus(userAttentService.gzStatus(finalUserId1, r.getUserId()));
                    }
                } else {
                    groupMemberListVO.setGzStatus("0");
                }
            }
            result.add(groupMemberListVO);
        });
        return result;
    }

    @Override
    public void sqrq(Long groupId) {
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
            throw new ServiceException("当前未登录,或登录已过期");
        }
        GroupMember gm = groupMemberMapper.selectMemberByGroupIdAndUserIdAll(groupId, finalUserId);
        if (gm == null) {
            GroupMember groupMember = new GroupMember();
            groupMember.setGroupId(groupId);
            groupMember.setUserId(finalUserId);
            groupMember.setStatus("1");
            this.insertGroupMember(groupMember);
        } else {
            if (!"2".equals(gm.getStatus())) {
                gm.setStatus("1");
                this.updateGroupMember(gm);
            }
        }
    }

    @Override
    public void sqrqsh(SqrqshDTO shDto) {
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
            throw new ServiceException("当前未登录,或登录已过期");
        }
        GroupMember groupMember = groupMemberMapper.selectMemberByGroupIdAndUserId(shDto.getGroupId(), shDto.getUserId());
        if (groupMember == null) {
            throw new ServiceException("没有相关数据");
        }
        groupMember.setStatus(shDto.getStatus());
        groupMember.setAuditUser(finalUserId);
        groupMember.setRemark(shDto.getRemark());
        groupMember.setUpdateTime(DateUtils.getNowDate());
        groupMember.setUpdateBy(finalUserId + "");
        groupMemberMapper.audit(groupMember);
    }

    @Override
    public GroupMember findOwnStatus(Long id, Long userId) {
        return groupMemberMapper.selectMemberByGroupIdAndUserIdAll(id, userId);
    }
}
